<!-- 支出单确认费用 -->
<template>
  <a-modal
    v-model:visible="showModal"
    @close="onClose"
    @before-ok="submitFn"
    title="付款"
    class="modal-dialog-wrapper"
    unmount-on-close
    :mask-closable="false"
    width="65vw"
  >
    <div class="fee-page">
      <div class="fee-list">
        <div style="margin: 0 20px">
          <div class="fee-tlt">
            <div class="tlt-name">
              <span class="tag-name">{{ form.costType == 1 ? '报销费用' : '支出费用' }}</span>
            </div>
            <div class="tlt-price">应付：{{ unitVal(form.amount, '元') }}</div>
          </div>
          <div style="margin-bottom: 24px">
            <div class="fee-msg">
              <div class="msg">
                <div class="msg-tlt"><span>支出单号：</span>{{ form.code || '-' }}</div>
                <div class="msg-tlt"><span>支出单类型：</span>{{ form.costTypeName || '-' }}</div>
                <div class="msg-tlt"><span>费用产生门店：</span>{{ form.feeOrgName || '-' }}</div>
                <div class="msg-tlt">
                  <span>关联工单号：</span>
                  <span v-if="form.workTypeName || form.workCode"> {{ form.workTypeName }} {{ form.workCode }} </span>
                  <span v-else>-</span>
                </div>
                <div class="msg-tlt">
                  <span>关联业务单号：</span>
                  <span v-if="form.businessTypeName || form.businessCode"> {{ form.businessTypeName }}{{ form.businessCode }} </span>
                  <span v-else>-</span>
                </div>
                <div class="msg-tlt"><span>申请备注：</span>{{ form.remark || '-' }}</div>
                <div class="msg-tlt"><span>收款方：</span>{{ form.customerName || '-' }}</div>
                <div class="msg-tlt"><span>收款方式：</span>{{ form.collectInExpWayName || '-' }}</div>
                <div class="msg-tlt"><span>账号：</span>{{ form.collectCard || '-' }}</div>
                <div class="msg-tlt" v-if="form.collectInExpWay == 1"><span>开户行：</span>{{ form.collectBackName || '-' }}</div>
              </div>
              <div class="msg-open" @click="showFee" v-if="form.feeList && !feeShow">展开 <icon-down /></div>
              <div class="msg-open" @click="showFee" v-if="form.feeList && feeShow">收起 <icon-up /></div>
            </div>
            <div class="line" v-if="feeShow">
              <div></div>
            </div>

            <template v-if="feeShow">
              <a-divider :margin="0" v-if="!feeShow && form.feeList" />
              <div class="fee-table" v-if="form.feeList">
                <div class="table-items" v-for="(item, index) in form.feeList" :key="index">
                  <div class="item-row">
                    <div class="item-pro">{{ item.itemName }}</div>
                    <div class="item-pro">付款</div>
                    <div class="item-pro">{{ unitVal(item.amount, '元') }}</div>
                    <div class="item-pro item-date">{{ item.occurTime }}</div>
                    <div class="item-pro">{{ item.carId ? item.licenseNo : '-' }}</div>
                    <div class="item-pro">{{ item.carId ? item.carModel : '-' }}</div>
                  </div>
                </div>
              </div>
            </template>
            <div class="amount-all" v-if="feeShow">
              <div class="line">
                <div></div>
              </div>
              <div class="amount">待付：{{ unitVal(form.amount, '元') }} </div>
            </div>
          </div>
        </div>
        <div class="all-cost">
          <span>合计</span>
          <div class="cost-num">
            <div>应付：{{ unitVal(Math.abs(form.amount), '元') }}</div>
            <div style="margin-top: 16px">待付：{{ unitVal(Math.abs(form.amount), '元') }}</div>
          </div>
        </div>
      </div>

      <a-divider class="custom-divider" />

      <page-form :model="form" layout="vertical" ref="formRef" style="margin-top: 40px">
        <page-form-row>
          <page-form-item field="payTime" label="付款时间" :rules="[{ required: true, message: '请选择付款时间', trigger: 'change' }]">
            <i-date-picker class="w-full" v-model="form.payTime" show-time value-format="YYYY-MM-DD HH:mm:ss" />
          </page-form-item>
          <page-form-item
            field="payWay"
            label="付款方式"
            :rules="{ required: true, message: '请选择付款方式' }"
            labelTooltipContent="收/付款方式支持在系统管理菜单下的收支账号管理页面进行配置。"
          >
            <a-select placeholder="请选择" v-model="form.payWay" @change="typeChange" popup-container=".fee-page">
              <a-option v-for="item in paywayEnum" :key="item.way" :value="item.way" :label="item.wayStr" />
            </a-select>
          </page-form-item>
          <page-form-item
            field="payAccountId"
            label="付款账号"
            labelTooltipContent="收/付款账号支持在系统管理菜单下的收支账号管理页面进行配置。"
          >
            <a-select
              :placeholder="!form.payWay ? '请先选择付款方式' : '请选择'"
              v-model="form.payAccountId"
              :disabled="!form.payWay ? true : false"
              popup-container=".fee-page"
            >
              <a-option
                v-for="item in accountList"
                :key="item.id"
                :value="item.id"
                :label="item.account + (item.bank ? '_' + item.bank : '')"
              />
            </a-select>
          </page-form-item>
          <page-form-item label="付款金额">
            <i-input-number disabled v-model="form.amount" unit="元" :precision="2" />
          </page-form-item>
          <page-form-item label="财务打款备注" size="2/3">
            <a-textarea class="w-full" placeholder="请输入" :max-length="400" v-model="form.payRemark" auto-size />
          </page-form-item>
        </page-form-row>
        <page-form-row>
          <page-form-item label="打款凭证（支持上传jpg、png格式，最多上传10张）" size="1">
            <upload-img v-model="form.payFileIdList" :limit="10" />
          </page-form-item>
        </page-form-row>
      </page-form>
    </div>
    <template #footer>
      <a-space>
        <a-button @click="onClose">关闭</a-button>
        <a-button type="primary" @click="submitFn">确认</a-button>
      </a-space>
    </template>
  </a-modal>
</template>

<script setup name="OperateCostConfirmFees">
  import { Message } from '@arco-design/web-vue'
  import dayjs from 'dayjs'
  import { unitVal } from '@/utils'
  import { getAccountByPageData, getAccountByWayAndType } from '@/api/order/sOrder'
  import { payCostApi } from '@/api/business/operateCost'
  import { ref } from 'vue'

  const paywayEnum = ref([]) //收付款方式
  const accountList = ref([]) //收付款账号列表

  const formRef = ref(null)

  const feeShow = ref(true)
  const showFee = () => {
    feeShow.value = !feeShow.value
  }
  const form = ref({})
  // 打开弹窗
  const showModal = ref(false)
  const show = (val) => {
    // 重置
    resetForm()
    getType()
    showModal.value = true
    console.log(val, '12val')
    form.value = val
    form.value.payTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
    form.value.customerName =
      val.collectType == '1' ? val.collectStaffName : val.collectType == '2' ? val.collectIndustryName : val.collectSupplierName
  }
  const onClose = () => {
    showModal.value = false
  }
  const resetForm = () => {
    paywayEnum.value = []
    accountList.value = []
    form.value = {}
  }
  // 收付款类型
  const getType = () => {
    getAccountByPageData({ type: 1 }).then((res) => {
      // 过滤
      var data = res.result
      data.forEach((i) => {
        // 付款方式中的枚举需要过滤网络平台代收、网络平台待付、微信线上支付、押金抵扣、预授权
        if (i.way != '1' && i.way != '2' && i.way != '3' && i.way != '4' && i.way != '5') {
          paywayEnum.value.push(i)
        }
      })
      if (form.value.payWay) {
        typeChange(form.value.payWay)
      }
    })
  }
  // 账号
  const typeChange = (e) => {
    if (e) {
      form.value.payAccountId = ''
    }
    paywayEnum.value.forEach((el) => {
      if (el.way == e) {
        getAccountByWayAndType({ way: Number(el.way), type: 1 }).then((res) => {
          accountList.value = res.result
        })
      }
    })
  }
  defineExpose({
    show,
  })
  const emit = defineEmits(['feepageConfirm'])
  const validateFormFn = () => {
    return new Promise((resolve, reject) => {
      formRef.value.validate((error) => {
        if (error) {
          reject()
        } else {
          resolve()
        }
      })
    })
  }
  // 确认提交付款
  const submitFn = () => {
    return new Promise((resolve, reject) => {
      validateFormFn()
        .then(() => {
          const param = {
            id: form.value.id,
            payTime: form.value.payTime,
            payWay: form.value.payWay,
            payAccountId: form.value.payAccountId,
            payFileIdList: form.value.payFileIdList,
            payRemark: form.value.payRemark,
          }
          payCostApi(param)
            .then((res) => {
              Message.success(res.message)
              showModal.value = false
              emit('feepageConfirm')
            })
            .catch(() => {
              reject()
            })
        })
        .catch(() => {
          reject()
        })
    })
  }
</script>

<style lang="less" scoped>
  .fee-list {
    padding-top: 20px;
    border: 1px solid #f2f3f5;
    .fee-tlt {
      width: 100%;
      height: 40px;
      background-color: #f2f3f5;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .tlt-name {
        .tag-name {
          font-weight: 600;
          font-size: 14px;
          margin-left: 20px;
        }
      }
      .tlt-price {
        font-size: 14px;
        color: #1890ff;
        font-weight: 600;
        margin-right: 20px;
      }
    }
    .fee-msg {
      background: #fbfbfc;
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      width: 100%;
      padding-top: 20px;
      .msg {
        flex: 1;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        flex-wrap: wrap;
        margin-left: 20px;
        div {
          width: 33.33%;
          margin-bottom: 12px;
        }
        .msg-tlt {
          color: var(--color-neutral-10);
          span {
            color: var(--color-neutral-8);
          }
        }
        .times {
          width: 66.66%;
        }
      }
      .msg-open {
        color: #1890ff;
        font-size: 14px;
        margin-right: 20px;
        cursor: pointer;
      }
    }
    .line {
      width: 100%;
      background-color: #fbfbfc;
      div {
        width: calc(100% - 40px);
        margin-left: 20px;
        height: 1px;
        background-color: #f2f3f5;
      }
    }
    .amount-all {
      background-color: #fbfbfc;
      .amount {
        width: 100%;
        height: 62px;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding-right: 20px;
      }
    }
    .fee-table {
      background: #fbfbfc;
      padding: 24px;
      width: 100%;
      .table-items {
        .item-row {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          .item-pro {
            font-weight: 600;
            width: 200px;
            margin-bottom: 12px;
            cursor: pointer;
          }
          .item-pro-twice {
            font-weight: 400;
            margin-left: 14px;
          }
          .open-icon {
            margin-left: 2px;
          }
          .open-icon-close {
            transform: rotate(180deg);
          }
          .item-name {
            width: 30%;
          }
          .item-cost {
            width: 15%;
          }
          .item-amount {
            width: 15%;
            font-weight: 400;
          }
          .item-remark {
            width: 40%;
            overflow: hidden;
            text-overflow: ellipsis;
            -webkit-line-clamp: 1;
            display: -webkit-box;
            -webkit-box-orient: vertical;
          }
          .item-date {
            width: 300px;
          }
        }
      }
    }
  }
  .all-cost {
    font-weight: 600;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    background: #f2f3f5;
    height: 100px;
    .cost-num {
      color: #1890ff;
      display: flex;
      flex-direction: column;
      align-items: flex-end;
    }
  }
  // 弹窗添加的样式
  .fee-page {
    width: 100%;
    max-height: 70vh;
    position: relative;

    :deep(.arco-row) {
      margin-left: 0 !important;
      margin-right: 0 !important;
    }
    :deep(.arco-modal-body) {
      padding: 10px 80px !important;
    }
    :deep(.arco-col) {
      padding: 0 10px !important;
    }
  }
</style>
